<template>
  <el-form :model="searchForm" class="search-tool">
    <el-row :gutter="10" class="search-tool-form">
      <el-col :span="6">
        <el-form-item>
          <el-input
            v-model="searchForm.businessNo"
            placeholder="请输入业务流水号"
            clearable
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button>
                <SvgIcon name="search" @click="handleSearch" />
              </el-button>
            </template>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="searchForm.dateList"
              type="datetimerange"
              value-format="YYYY-MM-DD HH:ss:mm"
              range-separator="至"
              clearable
              start-placeholder="Call车时间"
              end-placeholder="Call车时间"
              @change="handleSearch"
            />
          </el-form-item>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item>
          <el-select v-model="searchForm.source" placeholder="请选择Call车类型" clearable @change="handleSearch">
            <el-option v-for="p in callTypeOptions" :key="p.dicCode" :label="p.dicName" :value="p.dicCode" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item>
          <el-select v-model="searchForm.status" placeholder="请选择Call车状态" clearable @change="handleSearch">
            <el-option v-for="p in callStatusOptions" :key="p.dicCode" :label="p.dicName" :value="p.dicCode" />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <div class="search-tool-btn">
      <div class="bill-list-top">
        <el-dropdown v-if="isOperateShow">
          <el-button type="primary"> 新 增 <SvgIcon style="margin-left: 10px" name="arrowDown" /> </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item v-if="btnList?.includes(16)" @click="handleAdd('2', true)"
                >新增返利Call车</el-dropdown-item
              >
              <el-dropdown-item v-if="btnList?.includes(17)" @click="handleAdd('1', true)"
                >新增库融Call车</el-dropdown-item
              >
              <el-dropdown-item v-if="btnList?.includes(64)" @click="handleAdd('3', true)"
                >新增现金Call车</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-button v-if="btnList?.includes(18)" style="margin-left: 15px" type="primary" @click="handleExport"
          >导出</el-button
        >
      </div>
    </div>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, defineEmits } from 'vue';
import useBtnAuth from '@/hooks/useBtnAuth';
import useDictList from '@/hooks/useDictList';

const callStatusOptions = useDictList('CALL_STATUS');
const callTypeOptions = useDictList('CAR_TYPE');
// 权限
const { btnList, isOperateShow } = useBtnAuth([16, 17, 64]);

interface SearchForm {
  businessNo: string;
  status: string | number;
  source: string | number;
  dateList: any;
}

const emit = defineEmits<{
  (e: 'handleSearch', searchForm: SearchForm): void;
  (e: 'handleAdd', params: any): void;
  (e: 'handleExport'): void;
}>();

const searchForm = reactive<SearchForm>({
  businessNo: '',
  status: '',
  source: '',
  dateList: [],
});

const handleAdd = (callType: string | number, isEdit: boolean) => {
  emit('handleAdd', { callType, isEdit });
};

const handleExport = () => {
  emit('handleExport');
};

const handleSearch = () => {
  emit('handleSearch', searchForm);
};
</script>

<style lang="scss" scoped>
.search-tool {
  display: flex;
  height: 40px;
  align-items: center;
  .search-tool-form {
    flex: 1;
  }
  ::v-deep(.el-form-item) {
    margin-bottom: 0px !important;
  }
  .search-tool-btn {
    width: 180px;
    display: flex;
    justify-content: end;
  }
}
</style>
